package com.hay.item.controller;

import com.hay.item.pojo.SpecGroup;
import com.hay.item.pojo.SpecParam;
import com.hay.item.service.SpecificationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: SpecificationController
 * @Author HuangYan
 * @Date: 2020/7/22 18:11
 * @Version 1.0
 */
@Controller
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    SpecificationService specificationService;

    /**
     * 根据商品品类查询组信息
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){

        List<SpecGroup> list = specificationService.queryGroupsByCid(cid);
        if (CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 根据组id查询组下面的参数列表
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "generic", required = false) Boolean generic,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        List<SpecParam> list = specificationService.queryParams(gid, cid, generic, searching);
        if (CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 添加新组
     * @param cid
     * @param name
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> addCategoryGroupsByCid(@RequestParam("cid") Long cid, @RequestParam("name") String name){
        // 如果name组名称为空，则参数错误
        if (StringUtils.isBlank(name)){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        specificationService.addCategoryGroupsByCid(cid, name);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改组名称
     * @param id
     * @param cid
     * @param name
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> editCategoryGroupsById(
            @RequestParam("id") Long id,
            @RequestParam("cid") Long cid,
            @RequestParam("name") String name
    ){
        // 如果name组名称为空，则参数错误
        if (StringUtils.isBlank(name)){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        specificationService.editCategoryGroupsByCid(id, cid, name);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除组
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteCategoryGroupsById(@PathVariable("id") Long id){
        specificationService.deleteCategoryGroupsById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加组写下面的参数信息
     * @param specParam
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> addGroupParam(@RequestBody() SpecParam specParam){
        specificationService.addGroupParam(specParam);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改组下面的参数信息
     * @param specParam
     */
    @PutMapping("param")
    public ResponseEntity<Void> editGroupParam(@RequestBody() SpecParam specParam){
        specificationService.editGroupParam(specParam);
        return ResponseEntity.ok().build();
    }

    /**
     *  删除组下面得到参数
     * @param id
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteGroupParamById(@PathVariable("id") Long id){
        specificationService.deleteGroupParamById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据cid查询规格参数组和规格参数
     * @param cid
     * @return
     */
    @GetMapping("/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupWithParamByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> list = specificationService.queryGroupWithParamByCid(cid);
        if (CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }
}
